package client.comm.baoding.ui

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Intent
import android.graphics.Color
import android.text.Layout
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import client.comm.commlib.toast.toast
import client.comm.baoding.R
import client.comm.baoding.api.bean.GoodsOrder
import client.comm.baoding.databinding.ActivityGoodsorderdetailBinding
import client.comm.baoding.databinding.LayoutGoodswrapitemBinding
import client.comm.baoding.dialog.MsgTitleDialog
import client.comm.baoding.ui.vm.GoodsOrderDetailModel
import client.comm.baoding.BaseMiddleActivity
import client.comm.baoding.utils.TextTool

class GoodsOrderDetailActivity :
    BaseMiddleActivity<GoodsOrderDetailModel, ActivityGoodsorderdetailBinding>() {
    override fun getLayout(): Int {
        return R.layout.activity_goodsorderdetail
    }

    override fun initViewModel(): GoodsOrderDetailModel = createViewModel()

    override fun initView() {
        binding.event = this
        binding.vm = viewModel
//        this.binding.emptyLayout.setOnClickListener {
//            viewModel.holdCall("", "")
//        }
        binding.incGoodsList.etOrderMark.apply {
            isEnabled = false
            setText("无")
            setTextColor(resources!!.getColor(R.color.hint_color))
        }


    }

    override fun initData() {
        val bean = intent.getParcelableExtra<GoodsOrder.Order>("bean")

        bean?.let {
            binding.bean = it

            if (it.address_list != null && it.address_list.size > 0) {
                binding.addr = it.address_list[0]
            }
            when (it.status) {
                0 -> {
                    binding.orderStatue = "待付款"
                }
                1 -> {
                    binding.orderStatue = "待发货"
                }
                2 -> {
                    binding.orderStatue = "待收货"
                }
                3 -> {
                    binding.orderStatue = "已完成"
                }
                4 -> {
                    binding.orderStatue = "失败"
                }
            }
//            viewModel.isShowSign.value = (it.status != 0 && it.status != 4) && it.goods_list[0].price >= 1000.0

            binding.commPriceIntegral.apply {
                comm_price_integral(
                    tvPriceUnit,
                    tvPrice,
                    it.actual_price,
//                    it.lpq_price,
                    tvIntegral,
                    it.integral,
                    tvIntegralDesc
                )
            }

            binding.incGoodsList.apply {
                this.price = "${bean.total_price}"
                this.integral = "${bean.integral}"

                if (bean.integral > 0) {
                    rlIntegral.visibility = View.VISIBLE
                }
                var sendDate = bean.send_date //
                this.giveStage = sendDate
                if (sendDate!!.isNotEmpty()) {
                    rlGiveStage.visibility = View.VISIBLE
                }

            }

            binding.itemWrap.removeAllViews()
            for (i in it.goods_list) {
                DataBindingUtil.inflate<LayoutGoodswrapitemBinding>(
                    LayoutInflater.from(this),
                    R.layout.layout_goodswrapitem,
                    binding.itemWrap,
                    false
                ).apply {
                    mImgLoader.load(i.goods_img).into(img)
                    this.bean = i

                    if (i.price <= 0) {
                        binding.incGoodsList.rlGoodAccount.visibility = View.GONE
                    }

                    commPriceIntegral.apply {
                        comm_price_integral_goods(
                            tvPriceUnit,
                            tvPrice,
                            i.price,
//                            i.lpq_price,
                            tvIntegral,
                            i.integral,
                            tvIntegralDesc
                        )
                    }

                    binding.itemWrap.addView(root)
                }
            }
        }
    }

    /**
     * 通用商品价格和积分格式化
     */
    fun comm_price_integral(
        tvPriceUnit: TextView,
        tvPrice: TextView,
        price: Double,
//        lpq_price: Double,
        tvIntegral: TextView,
        integral: Double,
        tvIntegralDesc: TextView
    ) {
        var is_price_show: Boolean = false
        var is_integral_unit_show: Boolean = false
        if (price > 0) {
            tvPrice.text = "${price}"
            is_price_show = true
            if (integral > 0) {
                tvIntegral.text = "+${integral}" //+ "积分"
                is_integral_unit_show = true
            } else {
                tvIntegral.text = ""
                is_integral_unit_show = false
            }

        } else {
            tvPrice.text = "${price}"
//            tvPrice.text = ""
            is_price_show = false
            if (integral > 0) {
                tvIntegral.text = "" + integral //+ "积分"
                is_integral_unit_show = true
            } else {
                tvIntegral.text = ""
                is_integral_unit_show = false
            }
        }

        if (is_price_show) {
            tvPriceUnit.visibility = View.VISIBLE
        } else {
            tvPriceUnit.visibility = View.GONE
        }

        if (is_integral_unit_show) {
            tvIntegralDesc.visibility = View.VISIBLE
        } else {
            tvIntegralDesc.visibility = View.GONE
        }


    }


    fun comm_price_integral_goods(
        tvPriceUnit: TextView,
        tvPrice: TextView,
        price: Double,
//        lpq_price: Double,
        tvIntegral: TextView,
        integral: Double,
        tvIntegralDesc: TextView
    ) {
        var is_price_show: Boolean = false
        var is_integral_unit_show: Boolean = false

        if (price > 0) {
            tvPrice.text = "${price}"
            is_price_show = true
            if (integral > 0) {
                tvIntegral.text = "+${integral}" //+ "积分"
                is_integral_unit_show = true
            } else {
                tvIntegral.text = ""
                is_integral_unit_show = false
            }

        } else {
            tvPrice.text = ""
            is_price_show = false
            if (integral > 0) {
                tvIntegral.text = "" + integral //+ "积分"
                is_integral_unit_show = true
            } else {
                tvIntegral.text = ""
                is_integral_unit_show = false
            }
        }

        if (is_price_show) {
            tvPriceUnit.visibility = View.VISIBLE
        } else {
            tvPriceUnit.visibility = View.GONE
        }

        if (is_integral_unit_show) {
            tvIntegralDesc.visibility = View.VISIBLE
        } else {
            tvIntegralDesc.visibility = View.GONE
        }

        tvPrice.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16F)
        tvIntegral.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16F)

        tvPriceUnit.setTextColor(resources!!.getColor(R.color.txt_color))
        tvPrice.setTextColor(resources!!.getColor(R.color.txt_color))
        tvIntegral.setTextColor(resources!!.getColor(R.color.txt_color))
        tvIntegralDesc.setTextColor(resources!!.getColor(R.color.txt_color))

    }


    override fun observerUI() {
//        viewModel.holdCall("", "")

        viewModel.cancleCall.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    toast("成功")
                    finish()
                }
            }
        })
        viewModel.qrshCall.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    toast("成功")
                    finish()
                }
            }
        })

    }

    fun selectAddr() {
//        startActivityForResult(Intent(this, AddrMangerActivity::class.java).apply {
//            putExtra("select", true)
//        }, 0x11)
    }

    fun copyStr(v: View, str: String) {
        if (!str.isNullOrEmpty()) {
            val cmb = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
            cmb.setPrimaryClip(ClipData.newPlainText(null, str))
            toast("已复制")
        } else {
            toast("复制失败")
        }
    }

    fun qrsh() {
        MsgTitleDialog(this).apply {
            title = "是否确认收货？"
            ok = {
                viewModel.qrshCall(this@GoodsOrderDetailActivity.binding.bean!!.order_id)

            }
            okTxt = "确认"
            cancleTxt = "取消"
        }.show()
    }

    fun cancleOrder() {
        MsgTitleDialog(this).apply {
            title = "是否删除订单？"
            ok = {
                viewModel.cancle(this@GoodsOrderDetailActivity.binding.bean!!.order_id)
            }
            okTxt = "删除"
            cancleTxt = "取消"
        }.show()
    }

    fun lookWl() {
        startActivity(Intent(this, WlDetailActivity::class.java).apply {
            putExtra("orderId", binding.bean!!.order_id)
        })

//        LookWlDialog(this).apply {
//            bean = this@GoodsOrderDetailActivity.binding.bean
//        }.show()
    }

    fun applyTuihuo() {
        startActivity(Intent(this, ApplyTuiKuanActivity::class.java).apply {
            putExtra("bean", binding.bean)
        })
    }

    fun goSign() {
        startActivity(Intent(this, PaiduiActivity::class.java).apply {
            putExtra("sendMoney", binding.bean!!.send_money)
            putExtra("canSign", binding.bean!!.can_sign)
            putExtra("orderId", binding.bean!!.order_id)
            putExtra("nowMoney", binding.bean!!.now_money)
            putExtra("totalPrice", "${binding.bean!!.total_price}")
        })
    }
}